﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using ZHDJ.AppInterface.Common;
using ZHDJ.AppInterface.Models;
using ZHDJ.Core;
using ZHDJ.Core.Util;

namespace ZHDJ.AppInterface.Controllers
{
    /// <summary>
    /// 会议记录（三会一课）
    /// </summary>
    public class MeetingLectureRecordController : ApiController
    {
        #region 获取会议记录
        [HttpPost]
        public ReturnResult GetMeetingLectureRecord(dynamic obj)
        {
            int pageIndex = Convert.ToInt32(obj.pageIndex);
            int pageSize = Convert.ToInt32(obj.pageSize);
            string FID = Convert.ToString(obj.FID);
            DataTable dt = InterfaceHelper.GetMeetingLectureRecord(pageIndex, pageSize, FID);
            IList<PartyMeetingLectureRecord> list = DataTableToListModel<PartyMeetingLectureRecord>.ConvertToModel(dt);

            foreach (PartyMeetingLectureRecord model in list)
            {
                DataTable dtfile = InterfaceHelper.GetPartyFileByFID(model.ID);
                List<string> newlist = new List<string>();
                foreach (DataRow item in dtfile.Rows)
                {
                    newlist.Add(item["Url"].ToString());
                }
                model.PicList = newlist;
            }

            return new ReturnResult()
            {
                code = "200",
                result = list,
                msg = ""
            };
        }
        #endregion

        #region 获取单挑会议记录
        [HttpPost]
        public ReturnResult GetMeetingLectureRecordModel(dynamic obj)
        {
            string ID = Convert.ToString(obj.ID);
            PartyMeetingLectureRecord model = InterfaceHelper.GetMeetingLectureRecordModel(ID);
            return new ReturnResult()
            {
                code = "200",
                result = model,
                msg = "success"
            };
        }
        #endregion

        #region 保存
        public ReturnResult Save(dynamic obj)
        {
            ReturnResult model = new ReturnResult();
            PartyMeetingLectureRecord record = new PartyMeetingLectureRecord();

            try
            {
                record.ID = StringHelper.NewID;
                record.FID = Convert.ToString(obj.FID);
                record.Record = Convert.ToString(obj.content);
                record.CreateBy = Convert.ToString(obj.CreateBy);
                record.CreateTime = DateTime.Now;
                InterfaceHelper.MeetingLectureRecordSave(record);
                //批量添加文件表
                string[] array = Convert.ToString(obj.imgString).TrimEnd(',').Split(',');
                for (int i = 0; i < array.Length; i++)
                {
                    PartyFile file = new PartyFile();
                    file.ID = StringHelper.NewID;
                    file.FID = record.ID;
                    file.Title = "会议记录" + i.ToString();
                    file.Extension = ".jpg";
                    file.Description = "会议记录" + i.ToString();
                    file.Size = 0;
                    file.Url = array[i];
                    file.CreateTime = DateTime.Now;
                    file.CreateBy = record.CreateBy;
                    file.ModifyTime = DateTime.Now;
                    file.ModifyBy = record.CreateBy;
                    InterfaceHelper.AddFileTalbe(file);
                }

                model.code = "200";
                model.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                model.code = "201";
                model.msg = "保存失败！";
            }
            return model;
        }
        #endregion
    }
}
